package org.darcy.service.init;

import java.sql.Connection;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class InitSqlService {

	@Autowired
	private SqlSessionFactory sqlSessionFactory;

	/**
	 * 定时任务执行sql
	 */
	public void dailyTask() {
		try {
			SqlSession sqlSession = sqlSessionFactory.openSession();
			ClassPathResource resource = new ClassPathResource("mybatis/sql/dailyTask.sql");
			Connection conn = sqlSession.getConnection();
			EncodedResource er = new EncodedResource(resource, "utf-8");
			ScriptUtils.executeSqlScript(conn, er);
		} catch (Exception e) {
			log.error("定时任务执行sql失败", e);
		}
		log.info("定时任务执行sql成功");
	}
}
